import RPi.GPIO as GPIO
import time
import threading

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)


GPIO.output(16,0)
GPIO.output(20,0)
GPIO.output(21,0)

DJ_delay = 0.0015
DJ_buf = [[0,0],[0,1],[1,1],[1,0]]
global DJ_i
DJ_i = 0
DJ_size = 350
global DJ_jugg_flag
DJ_jugg_flag = 0

def zz():
    global DJ_i
    time.sleep(DJ_delay)
    GPIO.output(16,DJ_buf[DJ_i][0])
    GPIO.output(20,1)
    GPIO.output(21,DJ_buf[DJ_i][1])
    DJ_i = DJ_i + 1
    if DJ_i == 4:
        DJ_i = 0

def fz():
    global DJ_i
    time.sleep(DJ_delay)
    GPIO.output(16,DJ_buf[DJ_i][0])
    GPIO.output(20,1)
    GPIO.output(21,DJ_buf[DJ_i][1])
    DJ_i = DJ_i - 1
    if DJ_i == -1:
        DJ_i = 3

def DJ_jugg():
    global DJ_jugg_flag
    DJ_jugg_flag = 0
    DJ_flag = 0
    
    while True:
        GPIO.output(16,0)
        GPIO.output(20,0)
        GPIO.output(21,0)
        while DJ_jugg_flag == 1:
            if DJ_flag < DJ_size:
                zz()
                DJ_flag = DJ_flag +1
            else:
                DJ_flag = DJ_size
                DJ_jugg_flag = 0
        while DJ_jugg_flag == 2:
            if DJ_flag > -DJ_size:
                fz()
                DJ_flag = DJ_flag -1
            else:
                DJ_flag = -DJ_size
                DJ_jugg_flag = 0
        
        if DJ_jugg_flag == 3:           
            while DJ_flag > 0:
                fz()
                DJ_flag = DJ_flag -1
            while DJ_flag < 0:
                zz()
                DJ_flag = DJ_flag +1            
            DJ_flag = 0
            DJ_jugg_flag = 0
            
        if DJ_jugg_flag == 4:            
            DJ_flag = 0
            DJ_jugg_flag = 0
            
        if DJ_jugg_flag == 5:
            GPIO.output(16,0)
            GPIO.output(20,0)
            GPIO.output(21,0)
            break
        
def jugg():
    global DJ_jugg_flag
    thread_DJ = threading.Thread(target=DJ_jugg)
    thread_DJ.setDaemon(True)
    thread_DJ.start() 
    while True:        
        a = input(":")
        DJ_jugg_flag = int(a)        
        if DJ_jugg_flag == 5:            
            break

jugg()

GPIO.output(16,0)
GPIO.output(20,0)
GPIO.output(21,0)
time.sleep(1)
GPIO.cleanup()
